package com.resou.reader.reading;

import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.bookshelf.BookshelfRepository;
import com.resou.reader.data.readhistory.ReadHistoryRepository;
import com.resou.reader.data.reading.Reading;
import com.resou.reader.bookshelf.readhistory.datasupport.BookCollection;
import com.resou.reader.bookshelf.readhistory.datasupport.ReaderHistory;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.UserUtil;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.resou.reader.commom.Constant.APP;

public class ReadingPresenter extends ResouBasePresenter<ReadingContract.View> implements ReadingContract.Presenter {

    private final ReadingRepository mReadingRepository;
    private final BookshelfRepository mBookshelfRepository;
    private final ReadHistoryRepository mHistoryRepository;

    public ReadingPresenter(ReadingContract.View view) {
        super(view);
        mReadingRepository = Injection.provideReadingRepository();
        mBookshelfRepository = Injection.provideBookshelfRepository();
        mHistoryRepository = ReadHistoryRepository.getInstance();
    }

    /**
     * 加载章节目录
     *
     * @param bookId
     */
    public void loadChapterList(String bookId) {
        addCompositeDisposable(
                mReadingRepository.getChapterList(bookId, UserUtil.getToken(), APP)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(chapterItemBeans ->
                                        mView.setChapterList(chapterItemBeans),
                                throwable -> ToastUtil.makeLongToast(throwable.getMessage()))
        );
    }

    /**
     * 检测是否已添加到书架
     *
     * @param bookId
     * @return
     */
    @Override
    public boolean isAlreadyAddShelf(String bookId) {
        return mReadingRepository.checkIsInShelf(bookId);
    }

    /**
     * 添加到书架
     *
     * @param bookDetailBean
     */
    public void addToBookShelf(BookDetailBean bookDetailBean) {

    }

    /**
     * 获取阅读记录
     */
    public ReaderHistory getReadHistory(String bookId) {
        List<ReaderHistory> readHistory = mReadingRepository.getReadHistory(bookId);
        if (readHistory.size() > 0) {
            return readHistory.get(0);
        }
        return null;
    }


    /**
     * 保存最新信息到本地
     *
     //     * @param bookId      书的id
     //     * @param bookName    书的名称
     //     * @param chapterId   最新阅读的章节id
     //     * @param chapterName 章节名称
     //     * @param pageIndex   这一章的第几页
     //     * @param charIndex   这一章的第几个字符
     */

    public BookDetailBean loadBookDetailBean(String bookId) {
        return mReadingRepository.loadBookDetailBean(bookId);
    }

}
